Summary of Ways to Enable and Disable Local Network by C [3 Ways]

  • 2021-10-27 08:41:26
  • OfStack

This example summarizes how C # implements enabling and disabling local networks. Share it for your reference, as follows:

1) Use Hnetcfg. dll

Using Add Reference, importing Hnetcfg. dll into the project will generate three references, mainly using NETCONLib.

using NETCONLib in engineering;

Here is the code to implement:


NetSharingManagerClass netSharingMgr = new NetSharingManagerClass();
INetSharingEveryConnectionCollection connections = netSharingMgr.EnumEveryConnection;
foreach (INetConnection connection in connections)
{
INetConnectionProps connProps = netSharingMgr.get_NetConnectionProps(connection);
if (connProps.MediaType == tagNETCON_MEDIATYPE.NCM_LAN)
{
connection.Disconnect(); // Disable Network 
connection.Connect();  // Enable the network 
}
}

2) Use Shell32.dll

shell32. dll is an Windows shell Shell related application program interface dynamic link library file for opening web pages and files.

Using Add Reference, Shell32.dll is imported into the project.

In engineering, using Shell32;

Here is the code to implement:


const string discVerb = " Deactivate (&B)";
const string connVerb = " Enable (&A)";
Shell sh = new Shell32.Shell();
Folder folder;
Folder fd;
folder = sh.NameSpace(3);
foreach (FolderItem myItem in folder.Items())
{
if (myItem.Name == " Network connection ")
{
fd = (Folder)myItem.GetFolder;
// Disable Network 
foreach (FolderItem fi in fd.Items())
{
foreach (FolderItemVerb Fib in fi.Verbs())
{
if (Fib.Name == discVerb)
{
Fib.DoIt();
break;
}
}
Thread.Sleep(3000);
foreach (FolderItemVerb Fib in fi.Verbs())
{
// Enable the network 
if (Fib.Name == connVerb)
{
Fib.DoIt();
break;
}
}
}
}
}

3) Use setupapi. dll

setupapi. dll is a popular installer support related file

setupapi. dll cannot be imported into the project through Add Reference as the previous two, only DllImport can be used

There are many codes, and the main codes are posted


[DllImport("setupapi.dll")]
public static extern IntPtr
SetupDiGetClassDevsA(ref Guid ClassGuid, UInt32 Enumerator,
IntPtr hwndParent, UInt32 Flags);
[DllImport("setupapi.dll")]
public static extern IntPtr
SetupDiGetClassDevs(UInt32 ClassGuid, String e, IntPtr hwndParent, UInt32 Flags);
[DllImport("setupapi.dll")]
static extern Boolean
SetupDiEnumDeviceInfo(IntPtr DeviceInfoSet, UInt32 MemberIndex,
ref SP_DEVINFO_DATA DeviceInfoData);
 ...... 
uint NewNetStatus = 0;
if (newStatus)
NewNetStatus = DICS_ENABLE;
else
   NewNetStatus = DICS_DISABLE;
IntPtr NewDeviceInfoSet;
SP_DEVINFO_DATA spData = new SP_DEVINFO_DATA();
spData.cbSize = (uint)System.Runtime.InteropServices.Marshal.SizeOf(spData);
UInt32 RequiredSize = 0;
byte[] st1 = new byte[1024];
uint Data = 0;
NewDeviceInfoSet = SetupDiGetClassDevs(0, "PCI", IntPtr.Zero, DIGCF_PRESENT | DIGCF_ALLCLASSES);
bool bFound = false;
for (uint i = 0; SetupDiEnumDeviceInfo(NewDeviceInfoSet, i, ref spData); i++)
{
while (!SetupDiGetDeviceRegistryProperty(NewDeviceInfoSet, ref spData, SPDRP_HARDWAREID, ref Data, st1, 1024, ref RequiredSize))
{
}
string str = System.Text.Encoding.ASCII.GetString(st1); ;
char[] a ={ '/0' };
string[] strSPlit = str.Split(a, StringSplitOptions.RemoveEmptyEntries);
string HardId = @"PCI/VEN_10EC&DEV_8029&SUBSYS_00000000";
for (uint j = 0; j < strSPlit.Length; j++)
{
if (strSPlit[j] == HardId)
{
bFound = true;
break;
}
}
   if (bFound)
   break;
}
SP_PROPCHANGE_PARAMS spPropChangeParam = new SP_PROPCHANGE_PARAMS();
spPropChangeParam.Scope = DICS_FLAG_GLOBAL;
spPropChangeParam.StateChange = NewNetStatus;
spPropChangeParam.ClassInstallHeader.cbSize = (UInt32)System.Runtime.InteropServices.Marshal.SizeOf(spPropChangeParam.ClassInstallHeader);
spPropChangeParam.ClassInstallHeader.InstallFunction = DIF_PROPERTYCHANGE;
SetupDiSetClassInstallParams(NewDeviceInfoSet, ref spData, ref spPropChangeParam.ClassInstallHeader, System.Runtime.InteropServices.Marshal.SizeOf(spPropChangeParam));
SetupDiCallClassInstaller(DIF_PROPERTYCHANGE, NewDeviceInfoSet, ref spData);
SetupDiDestroyDeviceInfoList(NewDeviceInfoSet);

More readers interested in C # can check the topic of this site: "C # Form Operation Skills Summary", "C # Common Control Usage Tutorial", "WinForm Control Usage Summary", "C # Programming Thread Use Skills Summary", "C # Operating Excel Skills Summary", "XML File Operation Skills Summary in C #", "C # Data Structure and Algorithm Tutorial", "C # Array Operation Skills Summary" and "C # Object-Oriented Programming Introduction Tutorial"

I hope this article is helpful to everyone's C # programming.


Related articles: